package com.hulu.yec.stu.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.hulu.yec.common.Constant;
import com.hulu.yec.common.util.LogUtil;
import com.hulu.yec.stu.dao.ExamDao;
import com.hulu.yec.sys.user.entity.SysUserEntity;
import com.hulu.yec.teach.testpaper.entity.TestPaperEntity;
import com.hulu.yec.teach.testquestion.enetity.QuestionEntity;

/**
 * 考生进行考试
 * 
 * @author hf001
 * 2016年3月21日
 */
@WebServlet("/stu/GoingExamServlet")
public class GoingExamServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final ExamDao examDao = new ExamDao();

	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		if("initExamPaper".equals(command)){
			initExamPaper(request,response);
		}else if("endExam".equals(command)){
			recordScore(request,response);
		}else{
			throw new RuntimeException("请求非法!!!");
		}
		
	}

	private void recordScore(HttpServletRequest request,
			HttpServletResponse response) {
		int userId = ((SysUserEntity)request.getSession().getAttribute("user")).getId();
		String id = request.getParameter("paperId");
		TestPaperEntity testPaperEntity = examDao.getPaper(id);
		int score = getScore(request, response, testPaperEntity);
		int isPass = (int) request.getSession().getAttribute("planId");
		examDao.addRecord(userId,id,score,(Date)request.getSession().getAttribute("startDate"),new Date(),isPass);
	}

	/**
	 * 计算总得分
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private int getScore(HttpServletRequest request,
			HttpServletResponse response,TestPaperEntity testPaperEntity) {
			int choiceRightNum = getRightChoiceNumber(request,response,testPaperEntity.getSingleOptionNumber());
			int mulitRightNum = getRightMultiNumber(request,response,testPaperEntity.getMultiOptionNumber());
			int judgeRightNum = getRightJudgeNumber(request,response,testPaperEntity.getJudgeNumber());
			int totalScore = choiceRightNum * testPaperEntity.getSingleOptionEachPoint() + mulitRightNum * testPaperEntity.getMultiOptionEachPoint() + judgeRightNum * testPaperEntity.getJudgeEachPoint();
			return totalScore;
	}

	/**
	 * 得到判断题的正确数
	 * 
	 * @param request
	 * @param response
	 * @param judgeNumber
	 * @return
	 */
	private int getRightJudgeNumber(HttpServletRequest request,
			HttpServletResponse response, int judgeNumber) {
		int count=0;
		for (int i = 1; i <= judgeNumber; i++) {
			String qesId = request.getParameter("judge"+i);
			String answer = request.getParameter("judgeAnswer"+qesId);
			if(StringUtils.isNoneBlank(answer)&&examDao.isRight(Constant.JUDGETYPE,qesId,answer)){
				count++;
			}
		}
		return count;
	}

	/**
	 * 获取多选的正确答案数
	 * 
	 * @param request
	 * @param response
	 * @param multiOptionNumber
	 * @return
	 */
	private int getRightMultiNumber(HttpServletRequest request,
			HttpServletResponse response, int multiOptionNumber) {
		int count=0;
		for (int i = 1; i <= multiOptionNumber; i++) {
			String qesId = request.getParameter("multi"+i);
			String [] answer = request.getParameterValues("multiAnswer"+qesId);
			if( answer != null && answer.length>0){
				StringBuilder sb = new StringBuilder();
				for(String ans : answer){
					sb.append(ans);
				}
				if(examDao.isRight(Constant.MULTITYPE,qesId,sb.toString())){
					count++;
				}
			}
		}
		return count;
	}

	/**
	 * 得到单选题的正确数
	 * 
	 * @param request
	 * @param response
	 * @param singleOptionNumber
	 * @return
	 */
	private int getRightChoiceNumber(HttpServletRequest request,
			HttpServletResponse response, int singleOptionNumber) {
		int count=0;
		for (int i = 1; i <= singleOptionNumber; i++) {
			String qesId = request.getParameter("choices"+i);
			String answer = request.getParameter("choiceAnswer"+qesId);
			if(StringUtils.isNoneBlank(answer)&&examDao.isRight(Constant.CHOICETYPE,qesId,answer)){
				count++;
			}
		}
		return count;
	}

	/**
	 * 初始化试卷界面进行考试
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void initExamPaper(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("pageId");
		
		if(StringUtils.isNoneBlank(id)){
			TestPaperEntity testPaperEntity = examDao.getPaper(id);
			LogUtil.i(Constant.LOGGER, testPaperEntity.toString());
			List<QuestionEntity> choiceQuestions = examDao.getQuestion(Constant.CHOICETYPE,testPaperEntity.getSingleOptionNumber());
			List<QuestionEntity> multiQuestions = examDao.getQuestion(Constant.MULTITYPE,testPaperEntity.getMultiOptionNumber());
			List<QuestionEntity> judgeQuestions = examDao.getQuestion(Constant.JUDGETYPE,testPaperEntity.getJudgeNumber());
			LogUtil.i(Constant.LOGGER, choiceQuestions.toString());
			LogUtil.i(Constant.LOGGER, multiQuestions.toString());
			LogUtil.i(Constant.LOGGER, judgeQuestions.toString());
			request.setAttribute("testPaperEntity", testPaperEntity);
			request.setAttribute("choiceQuestions", choiceQuestions);
			request.setAttribute("multiQuestions", multiQuestions);
			request.setAttribute("judgeQuestions", judgeQuestions);
			Date startDate = new Date();
			request.getSession().setAttribute("startDate", startDate);
			request.getRequestDispatcher("/WEB-INF/view/stu/examPaper.jsp").forward(request, response);
		}
		
	}
	
	
	
}





